package com.xhtt.modules.cfg.schedule;

import com.xhtt.common.utils.SpringContextUtils;
import com.xhtt.modules.cfg.entity.CfgPumpInfo;
import com.xhtt.modules.cfg.entity.CfgPumpLog;
import com.xhtt.modules.cfg.entity.CfgPumpProduct;
import com.xhtt.modules.cfg.service.CfgPumpInfoService;
import com.xhtt.modules.cfg.service.CfgPumpLogService;
import com.xhtt.modules.cfg.service.CfgPumpProductService;
import com.xhtt.modules.cfg.service.impl.CfgPumpInfoServiceImpl;
import com.xhtt.modules.cfg.service.impl.CfgPumpLogServiceImpl;
import com.xhtt.modules.cfg.service.impl.CfgPumpProductServiceImpl;
import lombok.Data;
import org.apache.commons.lang.StringUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;

@Data
public class PumpLog implements Runnable {
    private CfgPumpInfoService pumpInfoService = SpringContextUtils.getBean(CfgPumpInfoServiceImpl.class);
    private CfgPumpProductService pumpProductService = SpringContextUtils.getBean(CfgPumpProductServiceImpl.class);
    private CfgPumpLogService pumpLogService = SpringContextUtils.getBean(CfgPumpLogServiceImpl.class);

    private CfgPumpInfo current;
    private CfgPumpInfo old;
    private List<CfgPumpProduct> oldProductList;
    private String deptUser;

    private PumpLog() {
    }

    public PumpLog(CfgPumpInfo current, CfgPumpInfo old, List<CfgPumpProduct> oldProductList, String deptUser) {
        this.current = current;
        this.old = old;
        this.oldProductList = CollectionUtils.isEmpty(oldProductList) ? new ArrayList<>() : oldProductList;
        this.deptUser = deptUser;
    }

    @Override
    public void run() {
        try {
            String rfidInfo = "";
            String comProductInfo = "";

            if (!ObjectUtils.nullSafeEquals(current.getRfid(), old.getRfid())) {
                rfidInfo = old.getRfid() + " → " + current.getRfid();
            }

            CfgPumpProduct curComProduct = null == current.getPumpProduct() ? new CfgPumpProduct() : current.getPumpProduct();
            CfgPumpProduct oldComProduct = oldProductList.stream().filter(p -> p.getType().intValue() == 1).findFirst().orElse(new CfgPumpProduct());
            if (!ObjectUtils.nullSafeEquals(curComProduct.getProductId(), oldComProduct.getProductId())) {
                comProductInfo = (null == oldComProduct.getName() ? "" : oldComProduct.getName()) + " → " + (null == curComProduct.getName() ? "" : curComProduct.getName());
            }

            if (StringUtils.isNotEmpty(rfidInfo) || StringUtils.isNotEmpty(comProductInfo)) {
                CfgPumpLog log = new CfgPumpLog();
                log.setPumpId(current.getId());
                log.setDeptUser(deptUser);
                log.setRfidInfo(rfidInfo);
                log.setComProductInfo(comProductInfo);
                pumpLogService.save(log);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
